<?php

namespace App\Models;

use App\Models\Traits\GoldCoinsTrait;
use App\Models\Traits\HasScopeInquire;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * App\Models\GoldCoin
 *
 * @property int $id
 * @property int $user_id
 * @property string|null $origin
 * @property string|null $result
 * @property string|null $money
 * @property string|null $remark
 * @property string|null $reason
 * @property string|null $data
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\User|null $user
 * @method static \Database\Factories\GoldCoinFactory factory(...$parameters)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin query()
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereData($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereOrigin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereReason($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereRemark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereResult($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereUserId($value)
 * @mixin \Eloquent
 * @method static Builder|GoldCoin index($scope = null)
 * @method static Builder|GoldCoin directPrize()
 * @method static Builder|GoldCoin goldCharge()
 * @method static Builder|GoldCoin inquire($condition = null)
 * @method static Builder|GoldCoin orderRefund()
 * @method static Builder|GoldCoin payGoldRecord()
 * @method static Builder|GoldCoin payOrder()
 * @method static Builder|GoldCoin resoldCost()
 * @method static Builder|GoldCoin resoldProfit()
 * @method static Builder|GoldCoin teamPrize()
 * @method static Builder|GoldCoin transferedIn()
 * @method static Builder|GoldCoin transferedOut()
 * @method static Builder|GoldCoin withdrawCharge()
 * @method static Builder|GoldCoin withdrawPrincipal()
 * @method static Builder|GoldCoin splitIncome()
 * @method static Builder|GoldCoin netProfit()
 * @method static Builder|GoldCoin resoldBeans()
 * @method static Builder|GoldCoin exceptionalReturn()
 * @method static Builder|GoldCoin unlockedGoldSubsidies()
 */
class Apple extends Model
{
    use HasFactory, HasScopeInquire;

    protected $guarded = ['id'];

	protected $casts = [
		'data' => 'json'
	];

	static array $inquireConditions = [
		'get_apples' => '获得苹果',
		'consume_apples' => '消耗苹果',
        'gift_apples' => '赠送苹果'
	];


	public function scopeTreeProducrApples(Builder $builder): Builder
	{
		return $builder->where('remark', 'tree_produce_apples');
	}

	public function scopeGardenProduceApples(Builder $builder): Builder
	{
		return $builder->where('remark', 'garden_produce_apples');
	}

	public function scopeGetApples(Builder $builder): Builder
	{
		return $builder->where('remark', '!=','gift_apples')
                    ->where('remark', '!=','apple_exchange_order')
                    ->where('remark', '!=','system_recall');
    }

    public function scopeConsumeApples(Builder $builder): Builder
    {

        return $builder->where(function (Builder $query) {
                $query->where('remark', 'gift_apples')
                    ->orWhere('remark', 'apple_exchange_order')
                    ->orWhere('remark', 'system_recall');
            });;
    }

	public function scopeGiftApples(Builder $builder): Builder
	{

		return $builder->where(function (Builder $query) {
                $query->where('remark', 'gift_apples')
                    ->orWhere('remark','get_apples');
            });;
	}

    public function scopeAppleExchangeOrder(Builder $builder): Builder
    {
        return $builder->where('remark', 'apple_exchange_order');
    }

  

    public function user(): BelongsTo
    {
    	return $this->belongsTo(User::class);
    }

    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }
}
